package com.mls.fivedistrict.controller.backend;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Message;
import com.mls.fivedistrict.server.MessageService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 消息表 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-06-26
 */
@RestController("backedMessageController")
@RequestMapping("/api/manager")
public class MessageController extends BaseController {
    @Autowired
    MessageService messageService;

    /**
     * 新增消息接口
     * @param message
     * @return
     */
    @PostMapping("/v1/message")
    public ServiceResponse addMessage(@RequestBody Message message){
        BeanValidator.check(message);
        boolean insert = messageService.insert(message);
        if(insert){
            return ServiceResponse.createSuccessResponse("添加成功");
        }else{
            return ServiceResponse.createErrorResponse("创建失败");
        }
    }

    /**
     * 消息删除接口
     * @param messageId
     * @return
     */
    @DeleteMapping("/v1/message/{messageId}")
    public ServiceResponse deleteMessage(@PathVariable Long messageId){
        Optional.ofNullable(messageId).orElseThrow(() -> new CustomerException("消息Id不能为空"));
        Message message = new Message();
        message.setId(messageId);
        message.setDelete(true);
        boolean delete = messageService.updateById(message);
        if(delete){
            return ServiceResponse.createSuccessResponse("删除成功");
        }else{
            return ServiceResponse.createErrorResponse("删除失败");
        }
    }

    /**
     * 消息编辑接口
     * @param messageId
     * @param message
     * @return
     */
    @PutMapping("/v1/message/{messageId}")
    public ServiceResponse updateMessage(@PathVariable Long messageId,@RequestBody Message message){
        Optional.ofNullable(messageId).orElseThrow(() -> new CustomerException("消息Id不能为空"));
        Optional.ofNullable(message).orElseThrow(() -> new CustomerException("更新的消息不能为空"));
        message.setId(messageId);
        boolean update = messageService.updateById(message);
        if(update){
            return ServiceResponse.createSuccessResponse("更新成功");
        }else{
            return ServiceResponse.createErrorResponse("更新失败");
        }
    }

    /**
     * 单个消息详情接口
     * @param messageId
     * @return
     */
    @GetMapping("/v1/message/{messageId}")
    public ServiceResponse getMessage(@PathVariable Long messageId){
        Optional.ofNullable(messageId).orElseThrow(() -> new CustomerException("消息Id不能为空"));
        Message message = messageService.selectById(messageId);
        if(null!=message){
            return  ServiceResponse.createSuccessResponse(message);
        }else{
            return ServiceResponse.createErrorResponse("经查没有该条消息");
        }
    }


    /**
     * 消息列表
     * @return
     */
    @GetMapping(value = "/v1/messages",produces = "application/json; charset=utf-8")
    public ServiceResponse getMessageList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        List list = messageService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo<Message>(list, 5);
        return  ServiceResponse.createSuccessResponse(pageInfo);
    }
}

